/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        int n=0;
        ListNode* tmp=head,*tail,*ret;
        while(tmp)
        {
            if(tmp->next==nullptr)
            {
               tail=tmp;
            }
            n++;
            tmp=tmp->next; 
                  
        }
        if(n==0 || n==1)
            return head;
        k=k%n;
        tail->next=head;
        tmp=head;
        for(int i=1;i<n-k;i++)
        {
            tmp=tmp->next;
        }
        ret=tmp->next;
        tmp->next=nullptr;
        return ret;
    }
};
